import { ActionTypes, LanguageActionTypes } from "./languageActions"

type languageCode = "zh" | "en" | string

export interface LanguageState {
	language: languageCode
	languageList: { name: string; code: string }[]
}

const defaultState: LanguageState = {
	language: "zh",
	languageList: [
		{
			name: "中文",
			code: "zh",
		},
		{
			name: "English",
			code: "en",
		},
	],
}

const languageReducer = (state = defaultState, action: LanguageActionTypes) => {
	switch (action.type) {
		case ActionTypes.CHANGE_LANGUAGE:
			return { ...state, language: action.payload }
		case ActionTypes.ADD_LANGUAGE:
			return {
				...state,
				languageList: [...state.languageList, action.payload],
			}
		default:
			return state
	}
}

export default languageReducer
